package com.chery.media.library.extend

import android.os.Bundle
import android.os.Parcelable
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.chery.media.library.router.Router
import java.io.Serializable

/**
 * @author MoYe_PanDeng
 * @ClassName:
 * @Description:ARouter 扩展
 * @date: 4/7/21
 */

fun String.navigation(needLogin: Boolean = false) {
    val routerPath = if (needLogin && getStorageBoolean("user_login_status") == false) {
        "${AppUtils.getAppPackageName()}.ui.login.LoginActivity"
    } else {
        "${AppUtils.getAppPackageName()}.${this}"
    }
    Router.newIntent(ActivityUtils.getTopActivity()).to(Class.forName(routerPath)).launch()
}


fun String.navigation(map: Map<String, Any?>, needLogin: Boolean = false) {
    val routerPath = if (needLogin && getStorageBoolean("user_login_status") == false) {
        "${AppUtils.getAppPackageName()}.ui.login.LoginActivity"
    } else {
        "${AppUtils.getAppPackageName()}.${this}"
    }
    val build = Router.newIntent(ActivityUtils.getTopActivity()).to(Class.forName(routerPath))
    map.forEach {
        build.withHelp(it.key, it.value)
    }
    build.launch()
}

fun String.navigation(vararg pair: Pair<String, Any?>, needLogin: Boolean = false) {
    val routerPath = if (needLogin && getStorageBoolean("user_login_status") == false) {
        "${AppUtils.getAppPackageName()}.ui.login.LoginActivity"
    } else {
        "${AppUtils.getAppPackageName()}.${this}"
    }
    val build = Router.newIntent(ActivityUtils.getTopActivity()).to(Class.forName(routerPath))
    pair.forEach {
        build.withHelp(it.first, it.second)
    }
    build.launch()
}

private fun Router.withHelp(key: String, second: Any?) = when (second) {
    is Byte -> this.putByte(key, second)
    is Char -> this.putChar(key, second)
    is Int -> this.putInt(key, second)
    is Long -> this.putLong(key, second)
    is Double -> this.putDouble(key, second)
    is Float -> this.putFloat(key, second)
    is Boolean -> this.putBoolean(key, second)
    is String -> this.putString(key, second)
    is Bundle -> this.putBundle(key, second)
    is ByteArray -> this.putByteArray(key, second)
    is CharArray -> this.putCharArray(key, second)
    is CharSequence -> this.putCharSequence(key, second)
    is Parcelable -> this.putParcelable(key, second)
    is Serializable -> this.putSerializable(key, second)
    else -> this
}



